package com.learn.controller;

import com.learn.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class RequestController {
    //原始方法接收参数
    @RequestMapping("/simpleRow")
    public String simpleRow(HttpServletRequest req){
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));

        System.out.println("name:"+name+" age:"+age);
        return "OK";
    }

    //springboot方法接受参数 required = false 表示参数可以不传 默认是true 不传递会报错400
    @RequestMapping("/simpleParam")
    public String simpleParam(@RequestParam(name = "name",required = true) String name, Integer age){
        System.out.println("name:"+name+" age:"+age);
        return "OK";
    }

    //springboot方法接受实体类型参数
    @RequestMapping("/complexParam")
    public String complexParam(User user){
        System.out.println(user);
        return "OK";
    }

    //springboot方法接受数组类型参数
    @RequestMapping("/ArrayParam")
    public String ArrayParam(String[] hobby){
        System.out.println(Arrays.toString( hobby));
        return "OK";
    }


    //springboot方法接受集合类型参数 需要@RequestParam注解绑定参数
    @RequestMapping("/ListParam")
    public String ListParam(@RequestParam List<String> hobby){
        System.out.println(hobby);
        return "OK";
    }

    //springboot方法接受日期类型参数
    @RequestMapping("/DateParam")
    public String DateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time){
        System.out.println(time);
        return "OK";
    }

    //springboot方法接受JSON类型参数 需要加上@RequestBody注解
    @RequestMapping("/JsonParam")
    public String JsonParam(@RequestBody User user){
        System.out.println(user);
        return "OK";
    }

    //路径参数
    @RequestMapping("/path/{id}")
    public String pathParam(@PathVariable Integer id){
        System.out.println(id);
        return "OK";
    }

    //路径参数多个
    @RequestMapping("/path/{id}/{name}")
    public String pathParam2(@PathVariable Integer id,@PathVariable String name){
        System.out.println(id);
        System.out.println(name);
        return "OK";
    }
}
